<template>
    <div class="product-navigate" @mouseenter="open()" @mouseleave="close()" :class="{pcclose: !pcActive,wapclose: !wapActive}">
        <slot></slot>
        <div class="product-navigate-arrow iconfont icon-shang" @click="wapActive = !wapActive"></div>
        <section class="product-navigate-main" @click="handleClick($event)">
            <div class="product-navigate-pc" :style="{ height: pcHeight}"></div>
            <div class="product-navigate-wap" :style="{ height: wapHeight}"></div>
            <div class="product-navigate-warp">
                <ul>
                    <li>
                        <a @click="toFavorites($event)" href="/product?type=collect" :class="{active: getLinkActive('collect')}">My favorites</a>
                        <!-- <nuxt-link :class="{active: getLinkActive('collect')}" to="/product?type=collect">My favorites</nuxt-link> -->
                    </li>
                    <!-- <li><nuxt-link to="/product?type=bestsellers">Bestsellers <svg-icon name="hot" /></nuxt-link></li> -->
                    <li v-if="newArrivals > 0">
                        <nuxt-link to="/product?type=new" :class="{active: getLinkActive('new')}">New arrivals
                            <svg-icon name="new" />
                        </nuxt-link>
                    </li>
                </ul>
                <div class="product-navigate-child" v-for="classify in classData" :key="classify.ID" :class="{childclose: childActive.indexOf(classify.ID) < 0 }">
                    <div class="product-navigate-child-height" :style="{ height: classify.child.length * 52 + 52 + 'px' }"></div>
                    <div class="product-navigate-box">
                        <h2>
                            <nuxt-link :to="'/product?classify=' + classify.classify_id" :class="{active: getLinkActive('classify',classify)}">{{ classify.name }}</nuxt-link>
                            <div class="product-navigate-arrow iconfont icon-shang" @click="openChild(classify)"></div>
                        </h2>
                        <p v-for="item in classify.child" :key="item.ID">
                            <strong>
                                <nuxt-link :to="'/product?classify=' + item.classify_id" :class="{active: getLinkActive('classify',item)}">{{ item.name }}</nuxt-link>
                            </strong>
                            <span v-for="childItem in item.child" :key="childItem.ID">
                                <nuxt-link :to="'/product?classify=' + childItem.classify_id" :class="{active: getLinkActive('classify',childItem)}">{{ childItem.name }}</nuxt-link>
                            </span>
                        </p>
                    </div>
                </div>
            </div>
        </section>
    </div>
</template>
<script>
import { mapGetters } from "vuex";
export default {
    name: "HeaderbarProduct",
    data () {
        return {
            pcActive: false,
            wapActive: true,
            childActive: [],
            timer: null,
            hoverEnterTime: 0,
            hoverLeaveTime: 100,
        };
    },
    computed: {
        ...mapGetters("common", {
            classData: "getProductClassify",
            newArrivals: 'getNewArrivals',
        }),
        ...mapGetters({
            isLogin: "getIsLogin",
        }),
        pcHeight () {
            const length = Math.max(2, ...this.classData.map(i => i.child.length + 1));
            const height = length * 40 + 40;
            return height + 'px';
            // this.navigateHeight = height;
        },
        wapHeight () {
            let length = this.newArrivals > 0 ? 2 : 1;
            this.classData.map(i => length += this.childActive.indexOf(i.ID) < 0 ? 0 : i.child.length)
            length += this.classData.length;

            return length * 52 + 'px';
        }
    },
    methods: {
        // 打开
        open (time) {
            const doOpen = () => {

                this.pcActive = true;
            }
            this.timer && clearTimeout(this.timer);
            if (Object.prototype.toString.call(time) == '[object Undefined]') {
                this.timer = setTimeout(doOpen, this.hoverEnterTime);
            } else {
                this.timer = setTimeout(doOpen, parseInt(time));
            }
        },
        // 关闭
        close (time) {
            const doClose = () => {
                this.pcActive = false;
            }
            this.timer && clearTimeout(this.timer);
            if (Object.prototype.toString.call(time) == '[object Undefined]') {
                this.timer = setTimeout(doClose, this.hoverLeaveTime);
            } else {
                this.timer = setTimeout(doClose, parseInt(time));
            }
        },
        // 打开子级
        openChild (classify) {
            if (this.childActive.indexOf(classify.ID) < 0) {
                this.childActive.push(classify.ID)
            } else {
                this.childActive = this.childActive.filter(i => i != classify.ID)
            }
        },
        // 子节点点击
        handleClick (event) {
            let target = event.target;
            target.closest("a") && this.close(0);
        },
        getLinkActive (type, classify) {

            const route = this.$route || {};
            const query = route.query || {}
            const path = route.path;

            if (!/\/product/.test(path)) {
                // 不是产品页
                return false;
            } else if (type == 'collect') {
                return query.type == 'collect';
            } else if (type == 'new') {
                return query.type == 'new';
            } else if (type == 'classify' && query.classify) {

                let isActive = false;
                let checkItem = (item, level) => {
                    if (item.classify_id == query.classify) {
                        isActive = true;
                    } else if (item.child && item.child.length > 0) {
                        item.child.map(i => checkItem(i, level + 1));
                    }
                }
                checkItem(classify, 0)
                return isActive;
            } else {
                return false;
            }
        },

        // 跳转到我喜欢的
        toFavorites (event) {

            event.stopPropagation();
            event.preventDefault();

            if (this.isLogin) {
                this.$router.push('/product?type=collect')
                this.close();
            } else {
                this.close();
                this.$emit("global:userLogin", () => this.$router.push('/product?type=collect'));
            }
        },
    },
    created () { },
};
</script>
<style lang="scss" scoped>
$activeColor: #ea4c89;

.product-navigate-main {
    position: fixed;
    left: 0;
    width: 100%;
    top: 78px;
    display: flex;
    flex-direction: row;
    overflow: hidden;

    .product-navigate-wap {
        display: none;
        transition: height 0.2s ease-in-out;
    }

    .product-navigate-pc {
        transition: height 0.2s ease-in-out;
    }

    .product-navigate-warp {
        display: flex;
        position: absolute;
        left: 0;
        top: 0;
        background-color: #fff;
        width: 100%;
        box-shadow: 0px 4px 8px 0px rgba(0, 0, 0, 0.12);
        padding: 0 266px;

        ul {
            padding: 12px 0;
            border-right: 1px solid #f2f2f2;
            margin-right: 12px;
            li {
                padding: 0 0 0 16px;
                cursor: pointer;
                width: 180px;
                height: 40px;
                line-height: 40px;
                background: #ffffff;
                font-size: 14px;
                color: #000;
                a {
                    display: flex;
                    align-items: center;
                    color: inherit;
                    font-weight: 600;
                    &.active {
                        color: $activeColor;
                    }
                }
                svg {
                    display: inline-block;
                    width: 18px;
                    height: 18px;
                    margin: 0 0 0 8px;
                }
                &:hover {
                    color: $activeColor;
                }
            }
        }
    }

    .product-navigate-child {
        padding: 12px 0;
        width: 180px;
        position: relative;

        .product-navigate-child-height {
            display: none;
        }

        .product-navigate-box {
            a {
                font-size: 14px;
                font-weight: 600;
                color: #000000;
                line-height: 40px;
                padding: 0 16px;
                height: 40px;
                cursor: pointer;
                &:hover {
                    color: $activeColor;
                }
                &.active {
                    color: $activeColor;
                }
            }
            p {
                strong {
                    font-weight: normal;
                }
                span {
                    margin-left: 12px;
                }
                a {
                    font-weight: 400;
                    color: #555;
                    font-size: 15px;
                }
            }
        }
    }
}

.product-navigate-arrow {
    position: absolute;
    display: none;
}

.product-navigate.pcclose .product-navigate-main .product-navigate-pc {
    height: 0 !important;
}

@media (max-width: 960px) {
    .product-navigate-arrow {
        position: absolute;
        right: 20px;
        top: 4px;
        width: 44px;
        height: 44px;
        line-height: 44px;
        text-align: center;
        display: block;
        color: #000;
        font-weight: normal;
        font-size: 14px;
        transition: transform 0.2s ease-in-out;
        &.active {
            transform: rotate(180deg);
        }
    }

    .product-navigate-main {
        position: relative;
        top: 0;
        padding: 0;
        flex-direction: column;

        .product-navigate-pc {
            display: none;
        }
        .product-navigate-wap {
            display: block;
        }

        .product-navigate-warp {
            padding: 0;
            flex-direction: column;
            box-shadow: none;
            & > ul {
                border: none;
                padding: 0;
                li {
                    padding: 0 14px;
                    width: 100%;
                    height: 52px;
                    line-height: 52px;
                    border-top: 1px solid #f0f0f0;
                    a {
                        font-weight: normal;
                        font-size: 16px;
                    }

                    &:first-child {
                        border: none;
                    }
                }
            }

            .product-navigate-child {
                width: 100%;
                padding: 0;
                border-top: 1px solid #f0f0f0;
                position: relative;
                overflow: hidden;

                .product-navigate-child-height {
                    display: block;
                    transition: height 0.2s ease-in-out;
                }

                .product-navigate-arrow {
                    right: 0;
                }
                .product-navigate-box {
                    position: absolute;
                    left: 0;
                    top: 0;
                    width: 100%;
                }

                &.childclose {
                    .product-navigate-child-height {
                        height: 52px !important;
                    }
                    .product-navigate-arrow {
                        transform: rotate(180deg);
                    }
                }

                a {
                    padding: 0 14px;
                    height: 52px;
                    line-height: 52px;
                    display: block;
                }
                h2 > a {
                    font-weight: normal;
                    font-size: 16px;

                    &.active {
                        // font-weight: bold;
                    }
                }
                p strong a {
                    padding: 0 28px;
                    font-size: 16px;
                }
            }
        }
    }

    .product-navigate.wapclose {
        .product-navigate-wap {
            height: 0px !important;
        }
        & > .product-navigate-arrow {
            transform: rotate(180deg);
        }
    }
}
</style>
